﻿namespace BM.Tools.WebControls.MForm
{
    using System;
    using System.Runtime.Serialization;
    using System.Security.Permissions;

    using BM.Tools.WebControls.MForm.Controls;

    /// <summary>
    /// An exception that is thrown when some functionality is used for an offspring type that it was not designed to be used with.
    /// </summary>
    [Serializable]
    public class InvalidOffspringTypeException : Exception
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="InvalidOffspringTypeException"/> class. 
        /// </summary>
        /// <param name="validType">
        /// The valid type.
        /// </param>
        public InvalidOffspringTypeException(OffspringType validType)
        {
            ValidType = validType;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="InvalidOffspringTypeException"/> class. 
        /// </summary>
        /// <param name="validType">
        /// The valid type.
        /// </param>
        /// <param name="innerException">
        /// The inner exception.
        /// </param>
        public InvalidOffspringTypeException(OffspringType validType, Exception innerException)
            : base(null, innerException)
        {
            ValidType = validType;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="InvalidOffspringTypeException"/> class.         
        /// </summary>
        public InvalidOffspringTypeException()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="InvalidOffspringTypeException"/> class.         
        /// </summary>
        /// <param name="message">
        /// The message that describes the error. 
        /// </param>
        public InvalidOffspringTypeException(string message)
            : base(message)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="InvalidOffspringTypeException"/> class.         
        /// </summary>
        /// <param name="message">
        /// The error message that explains the reason for the exception. 
        /// </param>
        /// <param name="innerException">
        /// The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified. 
        /// </param>
        public InvalidOffspringTypeException(string message, Exception innerException) : base(message, innerException)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="InvalidOffspringTypeException"/> class.         
        /// </summary>
        /// <param name="info">
        /// The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> that holds the serialized object data about the exception being thrown. 
        /// </param>
        /// <param name="context">
        /// The <see cref="T:System.Runtime.Serialization.StreamingContext"/> that contains contextual information about the source or destination. 
        /// </param>
        /// <exception cref="T:System.Runtime.Serialization.SerializationException">
        /// The class name is null or <see cref="P:System.Exception.HResult"/> is zero (0). 
        /// </exception>
        /// <exception cref="T:System.ArgumentNullException">
        /// The info parameter is null. 
        /// </exception>
        protected InvalidOffspringTypeException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }

        /// <summary>
        /// Gets ValidType.
        /// </summary>
        /// <value>
        /// The valid type that would not throw the exception.
        /// </value>
        public OffspringType ValidType
        {
            get; 
            private set;
        }

        /// <summary>
        /// Gets Message.
        /// </summary>
        /// <value>
        /// The message.
        /// </value>
        public override string Message
        {
            get
            {
                return "The only type that can be used for this functionality is " + ValidType + ". " + base.Message;
            }
        }

        /// <summary>
        /// When overridden in a derived class, sets the <see cref="T:System.Runtime.Serialization.SerializationInfo"/> with information about the exception.
        /// </summary>
        /// <param name="info">
        /// The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> that holds the serialized object data about the exception being thrown. 
        /// </param>
        /// <param name="context">
        /// The <see cref="T:System.Runtime.Serialization.StreamingContext"/> that contains contextual information about the source or destination. 
        /// </param>
        /// <exception cref="T:System.ArgumentNullException">
        /// The info parameter is a null reference (Nothing in Visual Basic). 
        /// </exception>
        [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]             
        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);
            info.AddValue("Message", Message);
            info.AddValue("ValidType", ValidType);
        }
    }
}